package com.sz.sboot.test;


import com.sz.sboot.Starter;
import com.sz.sboot.entity.TUser;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Starter.class) //加上启动类,测试的目录可以不相同
public class TsetRedis {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    //StringRedisTemplate继承了RedisTemplate,
    // redisTemplate需要进行序列化设置, 默认配置的jdk序列化会导致在客户端查看不了数据
    // (仍可使用内在函数存取修改, 只是查看不了), 为避免这种情况发生

    @Autowired
    @Qualifier("redisTemplate")
    private RedisTemplate redisTemplate;

    @Test
    public void test1(){ //存入String类型,并取出
        stringRedisTemplate.opsForValue().set("uname","易敏2号");
        String uname=stringRedisTemplate.opsForValue().get("uname");
        System.out.println(uname);
    }

    @Test
    public void test2(){ //存入String并设置有效时间
        stringRedisTemplate.opsForValue().set("yanzm","12345",3,TimeUnit.SECONDS); //3秒后失效
        try {
            Thread.sleep(3000);  //设置程序休眠3秒
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(stringRedisTemplate.opsForValue().get("yanzm"));
        //3秒后输入的值为null
    }

    @Test
    public void test3(){ //如果uname存在则末尾添加,如果不存在重新创建
        stringRedisTemplate.opsForValue().append("uname","qwe");
        System.out.println(stringRedisTemplate.opsForValue().get("uname"));
    }

    @Test
    public void test8() { //存入对象 ,对象必须序列化
        TUser tUser=new TUser(1,"易大师","123",3);
        redisTemplate.opsForValue().set("tuser",tUser);
        System.out.println(redisTemplate.opsForValue().get("tuser"));
    }

    @Test
    public void test9(){
        redisTemplate.opsForValue().append("uname","1");
    }

    @Test
    public void test4(){ //存入list类型
        stringRedisTemplate.opsForList().leftPushAll("list","aaa","bbb","ccc");
        stringRedisTemplate.opsForList().set("list",0,"易大师");
        System.out.println(stringRedisTemplate.opsForList().range("list",0,-1));
        System.out.println(stringRedisTemplate.opsForList().size("list"));
    }

    @Test
    public void test5(){ //存入hash类型
        Map map=new HashMap();
        map.put("u1","123");
        map.put("u2","234");
        stringRedisTemplate.opsForHash().putAll("h1",map);
        System.out.println(stringRedisTemplate.opsForHash().keys("h1"));
        System.out.println(stringRedisTemplate.opsForHash().values("h1"));
        System.out.println(stringRedisTemplate.opsForHash().entries("h1"));
    }

    @Test
    public void test6(){ //存入set类型
        stringRedisTemplate.opsForSet().add("s1","q123","w123","e123");
        System.out.println(stringRedisTemplate.opsForSet().members("s1"));

    }

    @Test
    public void test7(){ //存入zset类型
        stringRedisTemplate.opsForZSet().add("z1","张三",90);
        stringRedisTemplate.opsForZSet().add("z1","李四",20);
        stringRedisTemplate.opsForZSet().add("z1","王五",50);
        System.out.println(stringRedisTemplate.opsForZSet().range("z1",0,-1));
    }
}
